package com.atwisdom.star.controller;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ExcelImportHeader;
import com.atwisdom.star.common.vo.JsonResult;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.business.Impl.BusinessBslServiceImpl;
import com.atwisdom.star.core.bsl.event.EventUtil;
import com.atwisdom.star.core.dsl.modelClass.logic.LogicInstanceDslService;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.log.aop.StarAnnotation;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@AllArgsConstructor
@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController {
    private EventUtil eventUtil;
    private LogicInstanceDslService logicInstanceDslService;

    private BusinessBslServiceImpl  businessBslService;

    /**
     * 事件执行
     * @param eventData 接收参数据
     * @return 返回结果
     * @throws BusinessException  业务异常
     */
    @StarAnnotation
    @PostMapping("/eventExecute")
    public ResultInfo<?> eventExecute(@RequestBody JSONObject eventData) {
        String category = eventData.getString("category");
        String name = eventData.getString("name");
        JSONObject executeJson = eventData.getJSONObject("data");
        JSONObject executeResult = eventUtil.execute(category, name, executeJson);
        return JsonResult.success(executeResult);
    }

    /**
     * @param jsonObject 接收参数据
     * @return 返回结果
     * @throws BusinessException  业务异常
     */
    @StarAnnotation
    @PostMapping("/runEvent")
    public ResultInfo<?> runEvent(@RequestBody JSONObject jsonObject) {
        JSONObject runEvent = eventUtil.execute(jsonObject);
        return JsonResult.success(runEvent);
    }

    @ApiOperation(value = "上传实例文件", notes = "上传实例文件")
    @StarAnnotation
    @PostMapping(value = "/importInsFile", headers = ("content-type=multipart/*"), consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public  ResultInfo<?>  importInsFile(@RequestParam("file") MultipartFile file, @RequestParam("param") String param){
        System.out.println(param);
        ExcelImportHeader excelImportHeader = JSONObject.parseObject(param, ExcelImportHeader.class);
        boolean result = businessBslService.importInsFile(file,excelImportHeader);
        return JsonResult.success(result);
    }

    /**
     * 数采设备报警通知
     * @param jsonObject 接收参数据
     * @return 返回结果
     * @throws BusinessException  业务异常
     */
    @StarAnnotation
    @PostMapping("/equipAlarm")
    public ResultInfo<?> addEquipAlarm(@RequestBody JSONObject jsonObject) {
       return businessBslService.addEquipAlarm(jsonObject);
    }

    /**
     * 添加list的下级节点，并link到指定实例数据
     * @param data
     * @return 返回结果
     * @throws BusinessException  业务异常
     */
    @StarAnnotation
    @PostMapping("/addNodeAndLinkInstanceToListContainer")
    public ResultInfo<?> addNodeAndLinkInstanceToListContainer(@RequestBody JSONObject data) {
        return JsonResult.success(businessBslService.addNodeAndLinkInstanceToListContainer(data));
    }
}
